/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.lucava.boatskeeper.client.presenter.boat;

import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import java.util.ArrayList;
import java.util.List;
import net.customware.gwt.dispatch.client.DispatchAsync;
import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.place.Place;
import net.customware.gwt.presenter.client.place.PlaceRequest;
import net.customware.gwt.presenter.client.widget.WidgetDisplay;
import org.lucava.boatskeeper.client.MainLayout;
import org.lucava.boatskeeper.client.PlaceMapper;
import org.lucava.boatskeeper.client.event.AddEvent;
import org.lucava.boatskeeper.client.event.EditEvent;
import org.lucava.boatskeeper.client.presenter.ViewPresenter;
import org.lucava.boatskeeper.shared.Boat;
import org.lucava.boatskeeper.shared.rpc.boat.DeleteBoats;
import org.lucava.boatskeeper.shared.rpc.boat.DeleteBoatsResult;
import org.lucava.boatskeeper.shared.rpc.boat.GetBoats;
import org.lucava.boatskeeper.shared.rpc.boat.GetBoatsResult;

/**
 *
 * @author glenn
 */
public class BoatsPresenter extends ViewPresenter<BoatsPresenter.Display> {

    public interface Display extends WidgetDisplay {

        HasClickHandlers getAddButton();

        HasClickHandlers getDeleteButton();

        Column getEditColumn();

        void setData(List<Boat> data);

        List<Integer> getSelectedRows();
    }
    private List<Boat> boats;
    public static Place PLACE;

    public BoatsPresenter(DispatchAsync dispatcher, EventBus eventBus, Display display) {
        super(dispatcher, eventBus, display);

        fetchBoats();
        bind();
    }

    @Override
    protected void onBind() {
        display.getAddButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                eventBus.fireEvent(new AddEvent(PlaceMapper.EDIT_BOAT));
            }
        });

        display.getDeleteButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                deleteSelectedBoats();
            }
        });

        display.getEditColumn().setFieldUpdater(new FieldUpdater<Boat, String>() {
            @Override
            public void update(int index, Boat object, String value) {
                Integer id = object.getId();
                eventBus.fireEvent(new EditEvent(PlaceMapper.EDIT_BOAT, id));
            }
        });


        /*display.getList().addClickHandler(new ClickHandler() {
         @Override
         public void onClick(ClickEvent event) {
         int selectedRow = display.getClickedRow(event);

         if (selectedRow >= 0) {
         Integer id = boats.get(selectedRow).getId();
         eventBus.fireEvent(new EditEvent(PlaceMapper.EDIT_BOAT, id));
         }
         }
         });*/

    }

    public void setBoats(List<Boat> boats) {
        this.boats = boats;
    }

    public Boat getBoat(int index) {
        return boats.get(index);
    }

    private void fetchBoats() {
        getDispatcher().execute(new GetBoats(), new AsyncCallback<GetBoatsResult>() {
            @Override
            public void onSuccess(GetBoatsResult result) {
                boats = result.getBoatList();
                refreshDisplay();
            }

            @Override
            public void onFailure(Throwable caught) {
                System.out.println(caught.getMessage());
                Window.alert(caught.getMessage());
            }
        });
    }

    private void deleteSelectedBoats() {
        List<Integer> selectedRows = display.getSelectedRows();


        getDispatcher().execute(new DeleteBoats(selectedRows), new AsyncCallback<DeleteBoatsResult>() {
            @Override
            public void onSuccess(DeleteBoatsResult result) {
                boats = result.getBoats();
                refreshDisplay();
            }

            @Override
            public void onFailure(Throwable caught) {
                Window.alert("Error deleting selected contacts " + caught.getMessage());
            }
        });

    }

    @Override
    public void refreshDisplay() {

        display.setData(boats);

    }

    @Override
    protected void onPlaceRequest(PlaceRequest request) {
        fetchBoats();
    }

    @Override
    public Place getPlace() {
        return PLACE;
    }
}